package simulaco.business;

import gminet.infra.business.BusinessException;
import gminet.infra.business.BusinessObject;
import gminet.infra.db.Transaction;
import gminet.infra.db.TransactionException;
import gminet.infra.toolkit.StringToolkit;

import java.sql.Connection;
import java.util.List;

import simulaco.dao.QuestaoDAO;
import simulaco.dto.ConcursoMateria;
import simulaco.dto.Questao;
import simulaco.dto.QuestaoComplemento;
import simulaco.dto.QuestaoComplementoTipo;
import simulaco.dto.Situacao;

public class QuestaoBusiness extends BusinessObject {

    public static Questao findByPK(Questao questao) throws TransactionException {

        return findByPK(questao.getConcurso().getCodigo(), questao.getMateria().getCodigo(), questao.getCodigo());
    }

    public static Questao findByPK(int concursoId, int materiaId, int questaoId) throws TransactionException {

        Questao questao = null;
        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            questao = QuestaoDAO.findByPK(concursoId, materiaId, questaoId, conn);
            if (questao != null) {
                questao.setListaItens(QuestaoItemBusiness.findByQuestao(questao, conn));
            }
        } finally {
            transaction.release();
        }
        return questao;
    }

    public static List<Questao> findByConcursoMateria(ConcursoMateria concursoMateria) throws TransactionException {

        return findByConcursoMateria(concursoMateria.getConcurso().getCodigo(), concursoMateria.getMateria()
                .getCodigo());
    }

    public static List<Questao> findByConcursoMateria(int concursoId, int materiaId) throws TransactionException {

        List<Questao> questoes = null;
        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            questoes = findByConcursoMateria(concursoId, materiaId, conn);
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
        return questoes;
    }

    protected static List<Questao> findByConcursoMateria(int concursoId, int materiaId, Connection conn) throws TransactionException {

        List<Questao> questoes = null;
        questoes = QuestaoDAO.findByConcursoMateria(concursoId, materiaId, conn);
        return questoes;
    }

    public static Questao insert(Questao questao) throws BusinessException, TransactionException {

        Questao newQuestao = null;
        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            // COMPLEMENTO
            if (StringToolkit.isFill(questao.getComplemento().getTexto())) {
                QuestaoComplemento complemento = questao.getComplemento();
                complemento.setTipo(QuestaoComplementoTipo.TEXTO);
                complemento.setConcursoMateria(questao.getConcursoMateria());
                complemento = QuestaoComplementoBusiness.insert(complemento, conn);
                questao.setComplemento(complemento);
            }
            // QUESTAO
            questao.setCodigo(QuestaoDAO.getNextCodigo(conn, questao));
            if (questao.getSituacao().getCodigo() == 0) {
                questao.setSituacao(Situacao.DESABILITADO);
            }
            QuestaoDAO.insert(questao, conn);
            // QUESTAO ITENS
            QuestaoItemBusiness.insertFromQuestao(questao, conn);
            // COMMIT
            transaction.commit();
            //
            newQuestao = questao;
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
        return newQuestao;
    }

    public static void update(Questao questao) throws BusinessException, TransactionException {

        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            QuestaoDAO.update(questao, conn);
            // ALTERA ITENS
            QuestaoItemBusiness.updateFromQuestao(questao, conn);
            // ALTERA COMPLEMENTO
            QuestaoComplemento complemento = questao.getComplemento();
            if (complemento.getCodigo() > 0) {
                complemento.setTipo(QuestaoComplementoTipo.TEXTO);
                complemento.setConcursoMateria(questao.getConcursoMateria());
                QuestaoComplementoBusiness.update(complemento, conn);
            } else if (StringToolkit.isFill(complemento.getTexto())) {
                complemento.setTipo(QuestaoComplementoTipo.TEXTO);
                complemento.setConcursoMateria(questao.getConcursoMateria());
                complemento = QuestaoComplementoBusiness.insert(complemento, conn);
            }
            questao.setComplemento(complemento);
            // COMMIT
            transaction.commit();
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
    }

    public static void updateStatus(Questao questao) throws BusinessException, TransactionException {

        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            Questao auxQuestao = QuestaoDAO.findByPK(questao, conn);
            auxQuestao.setSituacao(questao.getSituacao());
            QuestaoDAO.update(auxQuestao, conn);
            // COMMIT
            transaction.commit();
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
    }

    public static void delete(Questao questao) throws BusinessException, TransactionException {

        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            // EXCLUI ITENS DA QUESTAO
            QuestaoItemBusiness.deleteByQuestao(questao, conn);
            // EXCLUI A QUESTAO
            QuestaoDAO.delete(questao, conn);
            // EXCLUI COMPLEMENTO
            QuestaoComplementoBusiness.delete(questao.getComplemento(), conn);
            //
            transaction.commit();
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
    }

}
